package com.snowleopard.findpeople.web.account;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.snowleopard.common.util.HibernateUtils;
import com.snowleopard.findpeople.entity.account.Role;
import com.snowleopard.findpeople.entity.account.User;
import com.snowleopard.findpeople.service.account.AccountManager;
import com.snowleopard.findpeople.web.CrudActionSupport;

@Namespace("/account")
@Results({ @Result(name = "home", location = "/search/search.action", type = "redirect") })
public class RegisterAccountAction extends CrudActionSupport<User> {

    private static final long serialVersionUID = 7467754610375426911L;
    private static final Logger log = LoggerFactory.getLogger(RegisterAccountAction.class);

    @Autowired
    private AccountManager accountManager;

    private User user;

    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }

    @Override
    public String list() throws Exception {
        // TODO Auto-generated method stub
        return SUCCESS;
    }

    @Override
    public String input() throws Exception {
        // accountManager.saveUser(entity);
        return null;
    }

    @Override
    public String save() throws Exception {
        List<Role> roleList = user.getRoleList();
        List<String> checkedRoleIds = new ArrayList<String>();
        checkedRoleIds.add("2");
        log.info(String.valueOf(roleList));
        if (null != roleList && roleList.size() < 1) {
            Role role = accountManager.getRole("2");
            roleList.add(role);
        }
        HibernateUtils.mergeByCheckedIds(roleList, checkedRoleIds, Role.class);
        accountManager.saveUser(user);
        addActionMessage("保存用户成功");
        return "home";
    }

    @Override
    public String delete() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    public User getUser() {
        return user;
    }

    public void setUser(final User user) {
        this.user = user;
    }

}
